<?php
/**
 * @version		$Id: home.php
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

/**
 * This models supports retrieving lists of contact categories.
 *
 * @package		Joomla.Administrator
 * @subpackage	com_je_gallery
 * @since		1.6
 */
class je_galleryModelGallery extends JModelList
{
	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @return	void
	 * @since	1.6
	 */
	protected function populateState()
	{
		$app = JFactory::getApplication();
		
		// List state information
		//$value = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'));
		$value = JRequest::getInt('limit', $app->getCfg('list_limit', 0));
		
		$this->setState('list.limit', $value = 20);
		
		$this->setState('list.start', JRequest::getVar('limitstart', 0, '', 'int'));
	}
	
	function getListQuery()
	{
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);
		
		$galleryInfo = self::getGalleryInfo();
		
		$query->select('a.*');
		$query->from('#__je_gallery_images a');
		$query->where('a.state = 1 AND a.galleryid="'.$galleryInfo->id.'"');
		$query->order('id DESC');
		
		return $query;
	}
	
	function getGalleryInfo()
	{
		$db = JFactory::getDbo();
		
		$galleryAlias = JRequest::getVar('gallery_alias');
		$query = "SELECT a.*, c.title as category_title FROM #__je_gallery_galleries a JOIN #__categories c ON a.catid = c.id WHERE a.alias = '$galleryAlias'";
		
		$db->setQuery($query);
		$result = $db->loadObject();
				
		return $result;
	}
}